<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

    <!--{include file='comm/header.tpl'}-->
    <title><!--{if isset($company_array)}-->修改大客户信息<!--{else}-->添加大客户信息<!--{/if}--></title>
    <!--{include file='comm/comm_editor.tpl'}-->
    <!--{include file='comm/comm_region.tpl'}-->
    <!--{include file="comm/icheck.tpl"}-->
    <!--引用div做成的下拉框效果样式-->
    <link rel="stylesheet" href="/quanliadmin/docroot/css/department_select.css" type="text/css" media="screen" />
    <!--引用div做成的下拉框效果js-->
    <script language="JavaScript" src="/quanliadmin/docroot/js/department_select.js" type="text/javascript"></script>

    <style type="text/css">
        .imageSite{
            position: absolute;
            left:280px;
            top:8px;
        }
    </style>

</head>

<body>

<div class="panel panel-default" style="padding: 5px;">

    <div class="panel-heading">
        <h2 style="" class="tal">正在操作：<!--{if isset($company_array)}-->修改<!--{else}-->添加<!--{/if}-->大客户信息</h2>
    </div>

    <div class="panel-body">

        <form id="item_form" onsubmit="return false;" class="form-horizontal">

            <div class="form-group">
                <label class="col-sm-2 control-label" for="company_name">
                    <span class="red">*</span>
                    公司名称：
                </label>
                <div class="col-sm-10">
                    <!--{if isset($company_array)}-->
                        <input class="form-control w300" type="text" id="company_name" value="<!--{$company_array.company_name}-->" name="company_name" readonly="readonly">
                    <!--{else}-->
                        <input class="form-control w300 fl" type="text" id="company_name" placeholder="请输入公司名称" name="company_name">
                        &nbsp;&nbsp;<button type="button" class="btn btn-primary" id="setMapBtn" onclick="company.searchCompany(1);">查询公司</button>
                    <!--{/if}-->
                    <input type="hidden" id="company_id" name="company_id" value="<!--{$company_array.company_id|default:''}-->">
                    <input type="hidden" id="properties" name="properties" value="<!--{$company_array.properties|default:''}-->">
                </div>                 
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label" for="connect_name">
                    <span class="red">*</span>
                    联系人：
                </label>
                <div class="col-sm-10">
                    <input id="connect_name" class="form-control w300" type="text" placeholder="请输入联系人" name="connect_name" value="<!--{$company_array.connect_name|default:''}-->">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label"><span class="red">*</span> 省市区：</label>
                <div class="col-sm-10">
                    <select class="form-control fl" id="province_list" name="province_id" style="width: 100px;margin-right: 5px;">
                        <option value="">请选择...</option>
                    </select>
                    <select class="form-control fl" id="city_list" name="city_id" style="width: 100px;margin-right: 5px;">
                        <option value="">请选择...</option>
                    </select>
                    <select class="form-control fl" id="county_list" name="country_id" style="width: 100px;">
                        <option value="">请选择...</option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label" for="address">
                    <span class="red">*</span>
                    地址：
                </label>
                <div class="col-sm-10">
                    <input id="address" style="float: left" class="form-control w300" type="text" placeholder="请输入地址" name="address" value="<!--{$company_array.address|default:''}-->">&nbsp;&nbsp;
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label" for="mobile">
                    <span class="red">*</span>
                    联系手机：
                </label>
                <div class="col-sm-10">
                    <input id="mobile" class="form-control w300" type="text" placeholder="请输入联系手机" name="mobile" value="<!--{$company_array.mobile|default:''}-->">
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label" for="departmentID">
                    <span class="red">*</span>
                    部门：
                </label>
                <div class="col-sm-10">

                    <div class="search_option">
                        <div class="search_option_drop" >
                                <span id="searchOption"  style="display:block;line-height:30px;">
                                    <!--{if isset( $company_array ) && !empty($company_array.departmentName)}-->
                                    <!--{$company_array.departmentName}-->
                                    <!--{else}-->
                                        请选择
                                    <!--{/if}-->
                                    <font class="imageSite"><img src="/quanliadmin/docroot/images/comm/selects.jpg"></font>
                                </span>
                        </div>

                        <ul class="search_option_panel"  id="searchPanel" style="display: none;z-index: 100">
                            <li class="childSelectImg" departmentID="0" style="margin-top:5px;margin-bottom:5px;margin-left: 0px;font-size: 13px;" >请选择</li>
                            <!--{$department_select_html}-->
                        </ul>

                        <input type="hidden" value="0" id="hdnSearchType" name="hdnSearchType">
                        <input type="hidden" value="" id="departmentID" name="departmentID">
                        <input type="hidden" value="" id="departmentName" name="departmentName">
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label" for="userID">
                    <span class="red">*</span>
                    销售员：
                </label>
                <div class="col-sm-10">
                    <select class="form-control w150" id="userID" name="userID">
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>

            <div class="form-group">
                <label class="col-sm-2 control-label" for="note">
                    备注：
                </label>
                <div class="col-sm-10">
                    <textarea id="note" class="form-control w300" rows="3" name="note"  placeholder="请输入备注"><!--{$company_array.note|default:''}--></textarea>
                </div>
            </div>

            <div class="form-group" >
                <label class="col-sm-2 control-label">&nbsp;</label>
                <div class="col-sm-10">
                    <input type="button" class="btn btn-primary" onclick="company.edit();" value="保存" />&nbsp;&nbsp;&nbsp;
                    <input type="button" class="btn btn-default" onclick="E.closeWindows();" value="关闭" />
                </div>
            </div>

        </form>
    </div>


</div>


<script type="text/javascript">

//编辑时给部门赋值
$(window).ready(function() {

    $(document).on('click', '#searchPanel li',function() {

        var departmentID = $(this).attr('departmentID');
        if (departmentID > 0) {

            company.searchUser( departmentID, '' );

        } else {
            var html = '<option value="">请选择</option>';
            $('#userID').html(html);
        }

    });

    <!--{if isset( $company_array )}-->

        var str =  '<!--{$company_array.departmentName}-->' + '<font class="imageSite"><img src="/quanliadmin/docroot/images/comm/selects.jpg"></font>';
        $('#searchOption').html(str);
        $('#departmentID').val('<!--{$company_array.departmentID}-->');
        $('#departmentName').val('<!--{$company_array.departmentName}-->');

        company.searchUser( <!--{$company_array.departmentID}-->, '<!--{$company_array.userID}-->' );

    <!--{/if}-->



});



var company = {

    searchUser: function( departmentID, userID ) {

        E.ajax_get({
            url: '/invokingAjax-backend/company/company.ajax?operFlg=2',
            data: {
                departmentID: departmentID
            },
            call: function( o ) {

                if (o.code == 200) {

                    var html = '<option value="">请选择</option>';
                    if ( o.data ) {
                        $.each( o.data, function( k, v ) {
                            if (userID == v.userID) {
                                html += '<option value="' + v.userID + '" selected>' + v.trueName + '</option>';
                            } else {
                                html += '<option value="' + v.userID + '">' + v.trueName + '</option>';
                            }
                        });
                    }
                    $('#userID').html(html);

                } else {

                    E.alert( o.message);

                }

            }
        });

    },
    //大客户添加的时候模糊查询公司名称 add by loreta.cheng 2015-06-10 start
    changeCompany: function(searchCompanyID){
        E.ajax_post({
            url: '/invokingAjax-backend/company/company.ajax?operFlg=100&act=search',
            data: {
                company_id: searchCompanyID
            },
            call: function( o ) {

                if (o.code == 200) {

                    if ( o.data ) {
                         $.each( o.data, function( k, v ) {
                           E.region.init({
                            pid: v.province_id,
                            cid: v.city_id,
                            ccid:v.country_id
                          });
                        $('#company_name').val(v.company_name);
                        $('#connect_name').val(v.connect_name);
                        $('#address').val(v.address); 
                        $('#website').val(v.website);
                        $('#mobile').val(v.mobile);
                        $('#phone').val(v.phone); 
                        $('#note').val(v.note);
                        $('#properties').val(v.properties);
                        $('#departmentID').val(v.departmentID);
                        $('#departmentName').val(v.departmentName);                        
                        $('#searchOption').html(v.departmentName); 
                        $('#employees_number_id').val(v.employees_number_id);
                        $('#industry_id').val(v.industry_id);
                        $('#nature_id').val(v.nature_id);
                        if (v.departmentID > 0) {
                                company.searchUser( v.departmentID, v.userID );
                            } else {
                                $('#userID').html('<option value="">请选择</option>');
                            }                        
                        });
                      
                    }

                } else {

                    E.alert( o.message);

                }
                E.popup.close();
            }
        });

    },
   /*
     *查询大客户信息
     */
    searchCompany: function(page) {
        var company_name="";
        if($("#company_name").val()!=undefined){
            company_name=$("#company_name").val();
        }
        if($("#searchCompanyName").val()!=undefined){
            company_name=$("#searchCompanyName").val();
        }

        E.loadding.open('正在查询，请稍候...');

        E.ajax_post({
            url:  "/invokingAjax-backend/company/company.ajax?operFlg=1&company_name="+company_name,
            data: {
                sortname: 'company_id',
                sortorder: 'ASC',
                rp: 10,
                page: page,
                list_type: 'general',
                show_type: 'company',
                way_type:'searchCompany'
            },
            call: function( o ) {

                E.loadding.close();
                if (o.data) {

                    var html_str = '<table><tr><td style="width: 80px;">公司名称：</td><td><input type="text" class="form-control w300" id="searchCompanyName" name="searchCompanyName" value='+o.company_name+'></span></td><td><input type="button" value="查询" id="searchBtn" class="btn btn-primary" onclick="company.searchCompany(1);" /></td></tr></table>';
                    html_str += '<table class="s">';
                    html_str += '<tbody>';

                    html_str += '<tr>';
                    html_str += '<th width="80">操作</th>';
                    html_str += '<th width="*">公司名称</th>';
                    html_str += '<th width="100">联系人</th>';
                    html_str += '<th width="100">联系方式</th>';
                    html_str += '<th width="180">部门</th>';
                    html_str += '<th width="100">销售员</th>';

                    html_str += '</tr>';
                    $.each(o.data, function(k, v) {

                        if (v.mobile) {
                            var contact_way = v.mobile;
                        } else {
                            var contact_way = v.phone;
                        }

                        html_str += '<tr>';
                        html_str += '<td class="tac"><a href="javascript:void(0)" class="company_line" val0='+v.company_id+' val1='+v.company_name+' val2='+ v.connect_name+' val3='+ contact_way +' onclick="company.changeCompany('+v.company_id+');">选择</a></td>';
                        html_str += '<td class="tac" style="text-align:left">' + v.company_name + '</td>';
                        html_str += '<td class="tac">' + v.connect_name + '</td>';
                        html_str += '<td class="tac">' + contact_way + '</td>';
                        html_str += '<td class="tac">' + v.departmentName + '</td>';
                        html_str += '<td class="tac">' + v.userName + '</td>';
                        html_str += '</tr>';

                    });
                    html_str += '</tbody>';
                    html_str += '</table>';


                    if (o.paging) {
                        html_str += o.paging;
                    }

                    E.popup.open({
                        content: html_str,
                        title: '大客户信息列表',
                        css: 'width: 800px;'
                    });

                }else{
                    var html_str = '<table><tr><td style="width: 80px;">公司名称：</td><td><input type="text" class="form-control w300" id="searchCompanyName" name="searchCompanyName" value='+o.company_name+'></span></td><td><input type="button" value="查询" id="searchBtn" class="btn btn-primary" onclick="company.searchCompany(1);" /></td></tr></table>';
                    html_str += '<table class="s">';
                    html_str += '<tbody>';

                    html_str += '<tr>';
                    html_str += '<th width="80">操作</th>';
                    html_str += '<th width="*">公司名称</th>';
                    html_str += '<th width="100">联系人</th>';
                    html_str += '<th width="100">联系方式</th>';
                    html_str += '<th width="180">部门</th>';
                    html_str += '<th width="100">销售员</th>';

                    html_str += '</tr>';
                    html_str += '</tbody>';
                    html_str += '</table>';
                    E.popup.open({
                        content: html_str,
                        title: '大客户信息列表',
                        css: 'width: 800px;'
                    });
                }

            }
        });

    },

    //大客户添加的时候模糊查询公司名称 add by loreta.cheng 2015-06-10 end
    edit: function() {

        var dt = E.getFormValues('item_form');
        var error_msg = '';

        if(E.isEmpty(dt.company_name))
            error_msg += '公司名称不能为空<br/>';

        if(E.isEmpty(dt.connect_name))
            error_msg += '联系人不能为空<br/>';

        if(E.isEmpty(dt.province_id))
            error_msg += '请选择省份<br/>';

        if(E.isEmpty(dt.city_id))
            error_msg += '请选择市区<br/>';

        if(E.isEmpty(dt.country_id))
            error_msg += '请选择地区<br/>';

        if(E.isEmpty(dt.address))
            error_msg += '地址不能为空<br/>';

        if(E.isEmpty(dt.mobile))
            error_msg += '联系手机不能为空<br/>';
        else if(!E.isMobile(dt.mobile))
            error_msg += '请输入正确的联系手机<br/>';

        if (E.isEmpty(dt.departmentID) || dt.departmentID == -1)
            error_msg += '请选择部门<br/>';

        if (E.isEmpty(dt.userID))
            error_msg += '请选择销售员<br/>';


        dt.province_name = $('#province_list').find('option:selected').text();
        dt.city_name = $('#city_list').find('option:selected').text();
        dt.country_name = $('#county_list').find('option:selected').text();
        if (dt.employees_number_id != 0) {
            dt.employees_number = $('#employees_number_id').find('option:selected').text();
        } else {
            dt.employees_number = '';
        }
        if (dt.industry_id != 0) {
            dt.industry = $('#industry_id').find('option:selected').text();
        } else {
            dt.industry = '';
        }
        if (dt.nature_id != 0) {
            dt.nature = $('#nature_id').find('option:selected').text();
        } else {
            dt.nature = '';
        }

        if (error_msg != '') {

            E.alert(error_msg);

        } else {

            E.confirm('您确认保存大客户信息吗？', function() {

                E.loadding.open('正在保存，请稍候...');

                E.ajax_post({
                    url: '/invokingAjax-backend/company/company.ajax?operFlg=100&act=edit',
                    data: dt,
                    call: function( o ) {

                        E.loadding.close();

                        if (o.code == 200) {

                            E.opener('company.load');

                            if (dt.company_id > 0) {
                                E.alert(o.message, 2, 'E.closeWindows');
                            } else {
                                document.getElementById('item_form').reset();
                                E.alert(o.message, 2);
                            }

                        } else {

                            E.alert(o.message);

                        }

                    }
                });

            });

        }

    }

};

//显示省市区
<!--{if !isset($company_array)}-->
    E.region.init();
<!--{else}-->
    E.region.init({
        pid: <!--{$company_array.province_id}-->,
        cid: <!--{$company_array.city_id}-->,
        ccid: <!--{$company_array.country_id}-->
    });
    $('#employees_number_id').val(<!--{$company_array.employees_number_id}-->);
    $('#industry_id').val(<!--{$company_array.industry_id}-->);
    $('#nature_id').val(<!--{$company_array.nature_id}-->);
<!--{/if}-->


</script>

</body>

</html>
